﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Model;
using Newtonsoft.Json.Linq;
using BLL;
using DAL;
using IDAL;
/***************************
 * 数据访问层
 * 基本信息
 * Author：JXL
 ***************************/ 
namespace EstateService.Controllers
{
    public class Basic_InfoController : ApiController
    {
        Factory<IBasic_Info, Basic_InfoDAL> fa = new Factory<IBasic_Info, Basic_InfoDAL>();
        Factory<ILoan_Info, Loan_infoDAL> fas = new Factory<ILoan_Info, Loan_infoDAL>();
        // GET api/values
        /// <summary>
        /// 基本信息列表
        /// </summary>
        /// <param name="pageindex"></param>
        /// <param name="pagesize"></param>
        /// <param name="wherestr"></param>
        /// <returns></returns>
        [HttpGet]
        [ActionName("listget")]
        public pagelist<Basic_infoss> listGet(int pageindex, int pagesize, string wherestr,int id)
        {
            return fa.bll().getlist(pageindex, pagesize, wherestr,id);
        }

        // GET api/values/5
        /// <summary>
        /// 获取单个实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Basic_infoss Get(int id)
        {
            return fa.bll().getmodel(id);
        }
        
        // POST api/values
        /// <summary>
        /// 进行添加动态数据类型
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public int Post([FromBody]dynamic value)
        {
            var id=fa.bll().getid();
            var name = Newtonsoft.Json.JsonConvert.DeserializeObject<string[]>(Convert.ToString(value.loan_Name));
            var tel=value.loan_TEL;
            var Card=value.loan_IdCard;
            List<Loan_Info> list=new List<Loan_Info> ();
            for(var i=0;i<name.Length;i++)
            {
                Loan_Info la=new Loan_Info (){
                    loan_Name=name[i],
                    loan_TEL=tel[i],
                    loan_IdCard=Card[i],
                    projectNumber=id,
                    createTime=DateTime.Now

                };
                list.Add(la);
            }
            Basic_Info bas = new Basic_Info();
            bas = Newtonsoft.Json.JsonConvert.DeserializeObject<Basic_Info>(Convert.ToString(value.Basic_Info));
            bas.projectNumber = id;
            bas.basic_CreateTime = DateTime.Now;
           var ii= fa.bll().insert<Basic_Info>(bas);
           var it = fas.bll().insert(list);
            if(ii>0&&it>0)
            {
                return 1;

            }
            else
            {
                return 0;
            }
           
        }

        // PUT api/values/5
        public void Put(int id, [FromBody]string value)
        {

        }
        [HttpPut]
        [ActionName("putsate")]
        public int putstate(int id)
        {
            return fa.bll().putstate(id);
        }
        // DELETE api/values/5
        public int Delete(int id)
        {
          return  fa.bll().delete(id.ToString());
        }
    }
}